<?php
    class taikhoan_controller extends CI_Controller{
        
        function __construct(){
            parent::__construct();
            $this->load->helper("url");
            $this->load->model('taikhoan_model');
            $this->load->database();
            session_start();
        }
        
        function dangnhap()
        {
            if($this->taikhoan_model->kiemtrataikhoan()==-1)
            {
                echo $this->taikhoan_model->kiemtrataikhoan();
            }
            else
            {
                $tkhoan = $this->taikhoan_model->kiemtrataikhoan();
                $_SESSION['mataikhoan'] = $tkhoan->row('MaTaiKhoan');
				$_SESSION['tentaikhoan'] = $tkhoan->row('TenDangNhap');
                $data['taikhoan'] = $this->taikhoan_model->laythongtintaikhoan($tkhoan->row('MaTaiKhoan'));
                echo $this->load->view('taikhoan_view', $data);
            }
        }
        
        function thoat()
        {
            session_destroy();
			//session_unregister($_SESSION['mataikhoan']);
			//session_unregister($_SESSION['tentaikhoan']);
            $this->load->view('dangnhap_view');
        }
        
        function dangky()
        {
            $this->load->view('dangky_view.php');
        }
        
        function dangkythanhcong()
        {
            $tendangnhap = $this->input->post('tendangnhap');
            $matkhau = $this->input->post('matkhaudk');
            $ngaysinh = $this->input->post('ngaysinh');
            $hoten = $this->input->post('hoten');
            $diachi = $this->input->post('diachi');
            $email = $this->input->post('email');
            $sodienthoai = $this->input->post('sodienthoai');
            $quyens = $this->input->post('checkbox');
            $anhdaidien = $_FILES['anhdaidien']['name'];
            $ngaysinh = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $ngaysinh)));
            
            if($_FILES['anhdaidien']['name'] != null)
                $this->taikhoan_model->danganh();
            
            $this->taikhoan_model->taotaikhoan($tendangnhap, $matkhau, $email, $ngaysinh, $hoten, $diachi, $sodienthoai, $anhdaidien);
            //hoàn thành thêm tài khoản
            
            $mataikhoan = $this->taikhoan_model->laytaikhoanmoi();
            
            if(count($quyens) == 2)
            {
                $this->taikhoan_model->themnguoidang($mataikhoan);
                $this->taikhoan_model->themnguoidanhgia($mataikhoan);
            }
            else
            {
                if($quyens[0] == 1)
                {
                    $this->taikhoan_model->themnguoidang($mataikhoan);
                }
                else
                {
                    $this->taikhoan_model->themnguoidanhgia($mataikhoan);
                }
            }
            redirect(base_url());
        }
        
        function chitietnguoidang()
        {
            $masp = $this->input->post('masp');
            $data['lienhe'] = $this->taikhoan = $this->taikhoan_model->chitietnguoidang($masp);
            echo $this->load->view('chitietnguoidang_view.php', $data);
        }
		
		function thoatquanly()
        {
     		$this->thoat();
            redirect(base_url());
        }
    }